/*
 * Java学习论坛
 * Copyright 2017 (C) All Rights Reserved.
 */
package cn.studyjava.verify.code12306.config.runner;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import cn.studyjava.verify.code12306.model.GroupSingleton;
import cn.studyjava.verify.code12306.util.FileUtil;
import cn.studyjava.verify.code12306.util.ThreadPoolExecutorUtil;

/**
 * <p>
 * Description: 项目加载成功执行一次
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History: -----------------------------------------------
 * @Date: 2017年8月7日 下午4:55:40
 * @author: zsljava
 * @version 1.0.0
 * @OP: 
 * Create -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
@Component
public class InitRunner implements CommandLineRunner {

	private Logger log = LoggerFactory.getLogger(InitRunner.class);

	@Value("${verify.folder.artworkDir}")
	private String artworkDir;
	
	@Value("${verify.folder.initDir}")
	private boolean initDir;
	
	/**
	 * @Title: run
	 * @Description: 项目启动加载一次
	 * @param args
	 * @throws Exception
	 * @see org.springframework.boot.CommandLineRunner#run(java.lang.String[])
	 */
	@Override
	public void run(String... args) throws Exception {
		// 生成字典
		if (GroupSingleton.getInstance().size() == 0) {
			log.debug("正在加载验证码图片");
			if(initDir){
				log.warn("删除验证码本地图片，将项目resources/defverify下的图片拷贝到{}目录中",artworkDir);
				File f = new File(artworkDir);
				f.delete();
			}
			
			ThreadPoolExecutorUtil threadPoolExecutorUtil = new ThreadPoolExecutorUtil();
			threadPoolExecutorUtil.execute(() -> {
				FileUtil.multiThreadReadFile(artworkDir);
			});
			threadPoolExecutorUtil.shutdown();
		}
		
	}

}
